package com.lifo.user.service.serviceimpl;

import com.lifo.user.bean.model.HfGoodsFavorites;
import com.lifo.user.dao.HfGoodsFavoritesMapper;
import com.lifo.user.service.HfGoodsFavoritesService;
import com.lifo.utils.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * Created by Administrator on 2018/6/26.
 */
@Slf4j
@Service
public class HfGoodsFavoritesServiceImpl implements HfGoodsFavoritesService {


    @Autowired
    private HfGoodsFavoritesMapper hfGoodsFavoritesMapper;

    /**
     *查询个人收藏
     * @param userId 用户id
     * @param startIndex 起始索引
     * @param endIndex  查询记录条数
     * @return
     */
    @Override
    public Page<HfGoodsFavorites> searchUserFavoritesGoods(int userId, int startIndex, int endIndex) {
        Page<HfGoodsFavorites> page = new Page<>();
        try{
            List<HfGoodsFavorites> hfGoodsFavorites = hfGoodsFavoritesMapper.searchUserFavoritesGoods(userId, startIndex, endIndex);
            page.setRows(hfGoodsFavorites);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询个人收藏时报错，异常信息为：===============》{}",e.getMessage());
        }
        return page;
    }

    /**
     * 保存个人收藏
     * @param hfGoodsFavorites
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer saveUserFavoritesGoods(HfGoodsFavorites hfGoodsFavorites) {
        try{
            return hfGoodsFavoritesMapper.saveUserFavoritesGoods(hfGoodsFavorites);
        }catch (Exception e){
            e.printStackTrace();
            log.error("保存个人收藏时报错，异常信息为：==================={}",e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return 0;
    }

    /**
     * 删除个人收藏
     * @param hfGoodsFavorites
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Boolean delUserFavoritesGoods(HfGoodsFavorites hfGoodsFavorites) {
        try{
            hfGoodsFavoritesMapper.delUserFavoritesGoods(hfGoodsFavorites);
            return true;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            log.error("删除个人收藏时报错，异常信息为：==================={}",e.getMessage());
        }
        return  false;
    }

    @Override
    public HfGoodsFavorites searchFavoritesGoodByHfGoodId(int hfGoodId,int userId) {
        HfGoodsFavorites hfGoodsFavorites = null;
        try{
            hfGoodsFavorites = hfGoodsFavoritesMapper.searchFavoritesGoodByHfGoodId(hfGoodId,userId);
        }catch (Exception e){
            log.error("删除个人收藏时报错，异常信息为：==================={}",e.getMessage());
            e.printStackTrace();
        }
        return hfGoodsFavorites;
    }
}
